package com.tang.crm.common;

import com.tang.crm.base.ResultInfo;
import com.tang.crm.exceptions.AuthException;
import com.tang.crm.exceptions.NoLoginException;
import com.tang.crm.exceptions.ParamsException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局处理异常
 */
@ResponseBody
@RestControllerAdvice(annotations = {Controller.class, RestController.class})
public class GlobalExceptionHandle {

    /**
     * 登录时参数异常
     * @param e
     * @return
     */
    @ExceptionHandler(ParamsException.class)
    public ResultInfo exceptionHandle(ParamsException e){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(e.getCode());
        resultInfo.setMsg(e.getMsg());
        return resultInfo;
    }

    @ExceptionHandler(NoLoginException.class)
    public ResultInfo noLoginExceptionHandle(NoLoginException e){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(e.getCode());
        resultInfo.setMsg(e.getMsg());
        return resultInfo;
    }

    /**
     * 捕获权限异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthException.class)
    public ResultInfo authException(AuthException e){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(e.getCode());
        resultInfo.setMsg(e.getMsg());
        return resultInfo;
    }

}
